package com.laoye.big_event.Service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.laoye.big_event.Mapper.ArticleMapper;
import com.laoye.big_event.Pojo.Article;
import com.laoye.big_event.Pojo.PageBean;
import com.laoye.big_event.Service.ArticleService;
import com.laoye.big_event.Utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public void add(Article article) {
        Map<String,Object> map=ThreadLocalUtil.get();
        int createUser= (int) map.get("id");
        article.setCreateUser(createUser);
        articleMapper.add(article);
    }

    @Override
    public PageBean<Article> list(int pageNum, int pageSize, Integer categoryId, String state) {
//        创建PageBean对象
        PageBean<Article> pageBean=new PageBean<>();
//        开启分页查询PageHelper，导入依赖
        PageHelper.startPage(pageNum,pageSize);
//        调用Mapper
        Map<String,Object> map=ThreadLocalUtil.get();
        Integer UserId = (Integer) map.get("id");
        List<Article> as=articleMapper.list(UserId,categoryId,state);
//        page中提供了方法，可以获取PageHelper分页查询后得到的总记录条数和当前页数据
        Page<Article> page= (Page<Article>) as;
//        把数据填充到PageBean对象中
        pageBean.setTotal(page.getTotal());
        pageBean.setItems(page.getResult());
        return pageBean;
    }

    @Override
    public Article detail(Integer id) {
        return articleMapper.detail(id);
    }

    @Override
    public void update(Article article) {
        articleMapper.update(article);
    }

    @Override
    public void delete(Integer id) {
        articleMapper.delete(id);
    }

//    @Override
//    public String getCategoryName(Integer categoryid) {
//      return   articleMapper.getCategoryName(categoryid);
//    }

}
